/*
 * MainFrame.java
 *
 * Created on 2007년 12월 22일 (토), 오후 8:24
 */
package PuriON;

import javax.media.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
/**
 *
 * @author  hyukminv
 */
public class PURION_Main extends javax.swing.JFrame  {
    
    boolean login = false;
    JDesktopPane desktop;
    String loginid = null;
    boolean ispro = false;
    /** Creates new form MainFrame */
    
    public PURION_Main() {

        // 컴포넌트 들을 초기화 합니다.
        this.initComponents();
        this.initLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        //this.initLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        
        this.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2 - this.getWidth()/2,
       (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 - this.getHeight()/2);     

    }
    public void setLoginID(String id) {
        loginid = id;
        login = true;
        jLabel1.setText("온라인(" + id + ")");
    }
    private void initLookAndFeel(String defaultlook) {
        final PURION_Main m = this;
        // Menu for the look and feels (lafs).
        UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
        ButtonGroup lafGroup = new ButtonGroup();

        JMenu optionsMenu = new JMenu("스타일");
        mainMenuBar.add(optionsMenu);
        for (int i = 0; i < lafs.length; i++) {
            JRadioButtonMenuItem rb = new JRadioButtonMenuItem(lafs[i].getName());
            optionsMenu.add(rb);
            rb.setSelected(UIManager.getLookAndFeel().getName().equals(lafs[i].getName()));
            rb.putClientProperty("UIKey", lafs[i]);
            rb.addItemListener(new ItemListener() {

                public void itemStateChanged(ItemEvent ae) {
                    JRadioButtonMenuItem rb2 = (JRadioButtonMenuItem) ae.getSource();
                    if (rb2.isSelected()) {
                        UIManager.LookAndFeelInfo info =
                                (UIManager.LookAndFeelInfo) rb2.getClientProperty("UIKey");
                        try {
                            System.out.println(info.getClassName());
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(m);
                        } catch (Exception e) {
                            System.err.println("unable to set UI " +
                                    e.getMessage());
                        }
                    }
                }
            });

            lafGroup.add(rb);
        }
        try {
            UIManager.setLookAndFeel(defaultlook);
            SwingUtilities.updateComponentTreeUI(m);
        } catch (Exception e) {
            System.err.println("unable to set UI " +
                    e.getMessage());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainDesktopPane = new javax.swing.JDesktopPane();
        mainMenuBar = new javax.swing.JMenuBar();
        basicMenu = new javax.swing.JMenu();
        loginMenuItem = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        quitMenuItem = new javax.swing.JMenuItem();
        shareMenu = new javax.swing.JMenu();
        shareMenuItem = new javax.swing.JMenuItem();
        statusMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem1 = new javax.swing.JMenuItem();
        listMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jToolBar1 = new javax.swing.JToolBar();
        searchBroadcasting = new javax.swing.JButton();
        startBroadcasting = new javax.swing.JButton();
        watchBroadcasting = new javax.swing.JButton();
        settingBroadcasting = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Puri-ON v1.0 beta");

        mainDesktopPane.setBackground(new java.awt.Color(255, 255, 255));
        mainDesktopPane.setDoubleBuffered(true);
        mainDesktopPane.setMinimumSize(new java.awt.Dimension(600, 400));
        getContentPane().add(mainDesktopPane, java.awt.BorderLayout.CENTER);

        mainMenuBar.setFont(mainMenuBar.getFont());

        basicMenu.setText("기본");

        loginMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        loginMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Login_S.png"))); // NOI18N
        loginMenuItem.setText("로그 인");
        loginMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginMenuItemActionPerformed(evt);
            }
        });
        basicMenu.add(loginMenuItem);

        jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Logout_S.png"))); // NOI18N
        jMenuItem3.setText("로그 아웃");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        basicMenu.add(jMenuItem3);
        basicMenu.add(jSeparator3);

        quitMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Symbol-Stop.png"))); // NOI18N
        quitMenuItem.setText("종료 하기");
        quitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitMenuItemActionPerformed(evt);
            }
        });
        basicMenu.add(quitMenuItem);

        mainMenuBar.add(basicMenu);

        shareMenu.setText("방송");

        shareMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        shareMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Upload_S.png"))); // NOI18N
        shareMenuItem.setText("방송 개설하기");
        shareMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        shareMenu.add(shareMenuItem);

        statusMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        statusMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/PanelSettings_S.png"))); // NOI18N
        statusMenuItem.setText("방송 설정하기");
        statusMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statusMenuItemActionPerformed(evt);
            }
        });
        shareMenu.add(statusMenuItem);
        shareMenu.add(jSeparator1);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/SearchComputer_S.png"))); // NOI18N
        jMenuItem1.setText("방송 검색하기");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        shareMenu.add(jMenuItem1);

        listMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        listMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Download_S.png"))); // NOI18N
        listMenuItem.setText("방송 시청하기");
        listMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slistMenuItemActionPerformed(evt);
            }
        });
        shareMenu.add(listMenuItem);

        mainMenuBar.add(shareMenu);

        jMenu1.setText("정보");

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Symbol-Information.png"))); // NOI18N
        jMenuItem2.setText("정보");
        jMenu1.add(jMenuItem2);

        mainMenuBar.add(jMenu1);

        getContentPane().add(mainMenuBar, java.awt.BorderLayout.PAGE_START);

        jToolBar1.setRollover(true);

        searchBroadcasting.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Search Computer.png"))); // NOI18N
        searchBroadcasting.setText("방송 검색하기");
        searchBroadcasting.setFocusable(false);
        searchBroadcasting.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        searchBroadcasting.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        searchBroadcasting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBroadcastingActionPerformed(evt);
            }
        });
        jToolBar1.add(searchBroadcasting);

        startBroadcasting.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Upload.png"))); // NOI18N
        startBroadcasting.setText("방송 개설하기");
        startBroadcasting.setFocusable(false);
        startBroadcasting.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startBroadcasting.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startBroadcasting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startBroadcastingActionPerformed(evt);
            }
        });
        jToolBar1.add(startBroadcasting);

        watchBroadcasting.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Download.png"))); // NOI18N
        watchBroadcasting.setText("방송 시청하기");
        watchBroadcasting.setFocusable(false);
        watchBroadcasting.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        watchBroadcasting.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(watchBroadcasting);

        settingBroadcasting.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Panel Settings.png"))); // NOI18N
        settingBroadcasting.setText("방송 설정하기");
        settingBroadcasting.setFocusable(false);
        settingBroadcasting.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        settingBroadcasting.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        settingBroadcasting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingBroadcastingActionPerformed(evt);
            }
        });
        jToolBar1.add(settingBroadcasting);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Login.png"))); // NOI18N
        jLabel1.setText("온라인(testpeer)");
        jLabel1.setIconTextGap(10);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(279, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
        );

        jToolBar1.add(jPanel1);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_END);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.LINE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void slistMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slistMenuItemActionPerformed
        
        // TODO add your handling code here:

        if(ispro==false)
            mainDesktopPane.add(new PURION_Consume(this));
        else
            JOptionPane.showMessageDialog(this,"공유 중입니다.");
        
        /*
        JFileChooser fileDlg = new JFileChooser();
        
        try {
            System.out.println(fileDlg.getSelectedFile().toString());
            mainDesktopPane.add(new PURION_Player(fileDlg.getSelectedFile().toURL()));
        }
        catch(MalformedURLException e) {
            System.err.println(e);
        }
        catch(Exception e) {
            System.err.println(e);
        }*/
}//GEN-LAST:event_slistMenuItemActionPerformed
    public void addComopnent(JInternalFrame i) {
        mainDesktopPane.add(i);
    }
    public void startProducer(String title)  {
        addComopnent(new PURION_Producer(this, title));
    }    
    public void startParticipant(String title, String ip, String port) {

//            mainDesktopPane.add(new PURION_Player("rtp://166.104.224.150:3000"));

    }
    
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        new PURION_Produce(this, true);
                ispro = true;
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void loginMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginMenuItemActionPerformed
        // TODO add your handling code here:
        if(login == false)
            new PURION_Login(this);
        else
            JOptionPane.showMessageDialog(this, loginid + "님은 이미 로그인 되어 있습니다. 다른 아이디로 로그인하려면 로그아웃하세요.");
    }//GEN-LAST:event_loginMenuItemActionPerformed

    private void quitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMenuItemActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_quitMenuItemActionPerformed

    private void startBroadcastingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startBroadcastingActionPerformed
        // TODO add your handling code here:
        new PURION_Produce(this, true);
        ispro = true;
}//GEN-LAST:event_startBroadcastingActionPerformed

    private void searchBroadcastingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBroadcastingActionPerformed
        // TODO add your handling code here:
        new PURION_Search(this, true);
}//GEN-LAST:event_searchBroadcastingActionPerformed

    private void statusMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statusMenuItemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_statusMenuItemActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        if(login==true)
        {
            this.setVisible(false);
            new PURION_Login(this);
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    new PURION_Search(this, true);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void settingBroadcastingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingBroadcastingActionPerformed
    // TODO add your handling code here:
    new PURION_Setting(this, true).setVisible(true);
}//GEN-LAST:event_settingBroadcastingActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        PURION_Main main = new PURION_Main();
        main.setVisible(true);
        //new PURION_Login(main);
    }
   
 // <editor-fold defaultstate="collapsed" desc="Generated Code">                             
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu basicMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem listMenuItem;
    private javax.swing.JMenuItem loginMenuItem;
    private javax.swing.JDesktopPane mainDesktopPane;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JButton searchBroadcasting;
    private javax.swing.JButton settingBroadcasting;
    private javax.swing.JMenu shareMenu;
    private javax.swing.JMenuItem shareMenuItem;
    private javax.swing.JButton startBroadcasting;
    private javax.swing.JMenuItem statusMenuItem;
    private javax.swing.JButton watchBroadcasting;
    // End of variables declaration//GEN-END:variables
// </editor-fold>      
}
